package org.example.data_stream;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
import org.apache.flink.connector.kafka.sink.KafkaSink;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 1. 创建topic
 * sh bin/kafka-topics.sh --create --bootstrap-server 192.168.116.131:9092 --replication-factor 1 --partitions 1 --topic clicks
 * sh bin/kafka-topics.sh --create --bootstrap-server 192.168.116.131:9092 --replication-factor 1 --partitions 1 --topic clicks_sink
 *
 * 2. 生产者
 * sh bin/kafka-console-producer.sh  --broker-list 192.168.116.131:9092 --topic clicks
 * 测试从kafka输入数据:
 * user01, ./home, 1660095536
 * user02, ./cart, 1660095536
 * user03, ./prod?id=100, 1660095536
 *
 * 3. 消费者
 * sh bin/kafka-console-consumer.sh --bootstrap-server 192.168.116.131:9092 --topic clicks_sink
 *
 * @author shenguangyang
 */
public class E11_SinkToKafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2. 从kafka中读取数据
        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("work01:9092")
                .setTopics("clicks")
                .setGroupId("consumer-group")
                .setStartingOffsets(OffsetsInitializer.latest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
                .setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
                .build();

        DataStreamSource<String> streamSource = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source");

        // 2. 用flink进行数据转换
        SingleOutputStreamOperator<String> result = streamSource.map((MapFunction<String, String>) value -> {
            String[] fields = value.split(",");
            return new Event(fields[0].trim(), fields[1].trim(), Long.valueOf(fields[2].trim())).toString();
        });

        // 3. 将结果数据写入到kafka中
        KafkaSink<String> sink = KafkaSink.<String>builder()
                .setBootstrapServers("work01:9092")
                .setRecordSerializer(KafkaRecordSerializationSchema.builder()
                        .setTopic("clicks_sink")
                        .setValueSerializationSchema(new SimpleStringSchema())
                        .setKeySerializationSchema(new SimpleStringSchema())
                        .build()
                )
                .build();
        result.sinkTo(sink);

        env.execute();
    }
}
